<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="128px">
      <el-form-item label="系统编码" prop="sysNum">
        <el-input
          v-model="queryParams.sysNum"
          placeholder="请输入系统编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系统名称" prop="sysName">
        <el-input
          v-model="queryParams.sysName"
          placeholder="请输入系统名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="系统有效性" prop="valid">
         <el-select
              v-model="queryParams.valid"
              placeholder="用户状态"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="系统启用时间" prop="enableTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.enableTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择系统启用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="系统停用时间" prop="disableTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.disableTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择系统停用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否允许代持账号" prop="allowShare">
        <el-select
              v-model="queryParams.allowShare"
              placeholder="用户状态"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in shareOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="支持门户登陆" prop="portalLogin">
        <el-select
              v-model="queryParams.portalLogin"
              placeholder="用户状态"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in supportSsoOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="集成用户方式" prop="integraType">
         <el-select
              v-model="queryParams.integraType"
              placeholder="用户状态"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in userIntegraOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sysList:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:sysList:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sysList:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sysList:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sysListList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="系统编码" align="center" prop="sysNum" />
      <el-table-column label="系统名称" align="center" prop="sysName" />
      <el-table-column label="系统有效性" align="center" prop="valid">
           <template slot-scope="scope">
              {{statusFormat(scope.row.valid)}}
            </template>
      </el-table-column>
      <el-table-column label="系统启用时间" align="center" prop="enableTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enableTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="系统停用时间" align="center" prop="disableTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.disableTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否允许代持账号" align="center" prop="allowShare" >
            <template slot-scope="scope">
              {{shareFormat(scope.row.allowShare)}}
            </template>
      </el-table-column>
      <el-table-column label="支持门户登陆" align="center" prop="portalLogin" >
             <template slot-scope="scope">
              {{supportSsoFormat(scope.row.portalLogin)}}
            </template>
      </el-table-column>
      <el-table-column label="集成用户方式" align="center" prop="integraType" >
            <template slot-scope="scope">
              {{userIntegraFormat(scope.row.integraType)}}
            </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="fieldSort" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sysList:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sysList:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改系统清单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="128px">
        <el-form-item label="系统编码" prop="sysNum"   v-if="title==='添加系统清单'">
          <el-input v-model="form.sysNum" placeholder="请输入系统编码"/>
        </el-form-item>
          <el-form-item label="系统编码" prop="sysNum" v-if="title==='修改系统清单'">
         <span type="text">{{form.sysNum}}</span>
        </el-form-item>
        <el-form-item label="系统名称" prop="sysName">
          <el-input v-model="form.sysName" placeholder="请输入系统名称" />
        </el-form-item>
        <el-form-item label="系统有效性" prop="valid">
            <el-select
              v-model="valid"
              placeholder="请选择系统有效性"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="系统启用时间" prop="enableTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.enableTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择系统启用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="系统停用时间" prop="disableTime" v-if="valid==2"> 
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.disableTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择系统停用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否允许代持账号" prop="allowShare">
            <el-select
              v-model="form.allowShare"
              placeholder="请选择是否允许代持账号"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in shareOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="支持门户登陆" prop="portalLogin">
            <el-select
              v-model="form.portalLogin"
              placeholder="请选择是否支持门户登陆"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in supportSsoOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="集成用户方式" prop="integraType">
            <el-select
              v-model="form.integraType"
              placeholder="请选择是否支持门户登陆"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in userIntegraOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="显示排序" prop="fieldSort">
          <el-input-number
            :precision="2"
            :step="0.1"
            v-model="form.fieldSort"
            placeholder="请输入排序"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSysList, getSysList, delSysList, addSysList, updateSysList, exportSysList } from "@/api/system/sysList";

export default {
  name: "SysList",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //有效性
      valid: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统清单表格数据
      sysListList: [],
      //状态数据字典
       statusOptions: [],
       //代持状态字典
       shareOptions: [],
      //支持单点登录
       supportSsoOptions:[],
       //用户集成方式
       userIntegraOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sysNum: null,
        sysName: null,
        valid: null,
        enableTime: null,
        disableTime: null,
        allowShare: null,
        portalLogin: null,
        integraType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sysNum: [
          { required: true, message: "系统编码不能为空", trigger: "blur" }
        ],
        sysName: [
          { required: true, message: "系统名称不能为空", trigger: "blur" }
        ],
        valid: [
          { required: true, message: "系统有效性不能为空", trigger: "blur" }
        ],
        allowShare: [
          { required: true, message: "是否允许代持账号不能为空", trigger: "blur" }
        ],
        enableTime: [
          { required: true, message: "系统启用时间不能为空", trigger: "blur" }
        ],
        portalLogin: [
          { required: true, message: "支持门户登陆不能为空", trigger: "blur" }
        ],
        integraType: [
          { required: true, message: "用户集成方式不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("outside_sys_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("outside_sys_share").then(response => {
      this.shareOptions = response.data;
    });
     this.getDicts("outside_support_sso").then(response => {
      this.supportSsoOptions = response.data;
    });
      this.getDicts("outside_user_integra").then(response => {
      this.userIntegraOptions = response.data;
    });
  
  },
  watch: {
     valid:{
      handler(newName, oldName) {
        console.log("新值： " + newName + ", 旧值：" + oldName)
         this.form.valid = this.valid;
      if(this.form.valid==="1"){
        this.form.disableTime = null;
        }
      }
    },
  },
  methods: {
    /** 查询系统清单列表 */
    getList() {
      this.loading = true;
      listSysList(this.queryParams).then(response => {
        this.sysListList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sysNum: null,
        sysName: null,
        valid: null,
        enableTime: null,
        disableTime: null,
        allowShare: null,
        portalLogin: null,
        integraType: null,
        fieldSort:null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.valid = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加系统清单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSysList(id).then(response => {
        this.form = response.data;
        this.valid = response.data.valid;
        this.open = true;
        this.title = "修改系统清单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSysList(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysList(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除系统清单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSysList(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有系统清单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSysList(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
     statusFormat(type){
      let option=this.statusOptions;
        for (let i = 0; i < option.length; i++) {
        if (option[i].dictValue==type){
          return option[i].dictLabel;
        }
      }
    },
    shareFormat(type){
      let option=this.shareOptions;
        for (let i = 0; i < option.length; i++) {
        if (option[i].dictValue==type){
          return option[i].dictLabel;
        }
      }
    },
    supportSsoFormat(type){
       let option=this.supportSsoOptions;
        for (let i = 0; i < option.length; i++) {
        if (option[i].dictValue==type){
          return option[i].dictLabel;
        }
      }
    },
    userIntegraFormat(type){
       let option=this.userIntegraOptions;
        for (let i = 0; i < option.length; i++) {
        if (option[i].dictValue==type){
          return option[i].dictLabel;
        }
      }
    },
  }
};
</script>
